package org.thinginitself.mixpath.dom;

import org.jaxen.JaxenException;
import org.jaxen.XPath;
import org.jaxen.dom.DOMXPath;
import org.thinginitself.mixpath.MiXPath;
import org.thinginitself.mixpath.MiXPathException;
import org.thinginitself.mixpath.MutableXPath;

import java.util.Map;

/**
 * @author Andrew Harrison
 * @version 1.0.0 16/05/2011
 */
public class DomMiXPath extends MiXPath {


    public DomMiXPath(Object context) {
        super(context);
    }

    public DomMiXPath() {
        super(DomHelp.newDocument());
    }

    protected MutableXPath createMiXPath(String xpath) throws MiXPathException {
        try {
            MutableXPath gen = new DomMutablePath(xpath);
            Map<String, String> namespaces = getNamespaces();
            for (String s : namespaces.keySet()) {
                gen.addNamespace(s, namespaces.get(s));
            }
            return gen;
        } catch (JaxenException e) {
            throw new MiXPathException(e);
        }
    }

    @Override
    protected XPath createXPath(String xpath) throws MiXPathException {
        try {
            XPath gen = new DOMXPath(xpath);
            Map<String, String> namespaces = getNamespaces();
            for (String s : namespaces.keySet()) {
                gen.addNamespace(s, namespaces.get(s));
            }
            return gen;
        } catch (JaxenException e) {
            throw new MiXPathException(e);
        }
    }

}
